package com.tuozixuan.onesql.ui;

import java.util.Arrays;
import java.util.List;
import java.util.Objects;

import org.apache.commons.lang3.StringUtils;

import com.tuozixuan.onesql.db.Column;
import com.tuozixuan.onesql.db.ColumnTypeEnum;
import com.tuozixuan.onesql.service.TableContext;
import com.tuozixuan.onesql.ui.constant.QueryFilterOperateEnum;

import javafx.collections.FXCollections;
import javafx.scene.Node;
import javafx.scene.control.ChoiceBox;
import javafx.scene.control.TextField;
import javafx.util.StringConverter;

public class ConditionFilterBoxItem {
    
    private ChoiceBox<Column> fieldChoiceBox;
    
    private ChoiceBox<QueryFilterOperateEnum> operateChoiceBox;
    
    private TextField valueTextField;
 
    public ConditionFilterBoxItem(TableContext tableContext) {
        List<Column> columnList = tableContext.getColumnList();
        fieldChoiceBox = new ChoiceBox<Column>(FXCollections.observableArrayList(columnList));
        fieldChoiceBox.converterProperty().set(new StringConverter<Column>() {

            @Override
            public Column fromString(String arg0) {
                return null;
            }

            @Override
            public String toString(Column column) {
                return column.getName();
            }
        });
        fieldChoiceBox.setId("queryField1");
        
        
        operateChoiceBox = new ChoiceBox<QueryFilterOperateEnum>(
                FXCollections.observableArrayList(QueryFilterOperateEnum.values()));
        operateChoiceBox.setId("queryOperate1");
        operateChoiceBox.converterProperty().set(new StringConverter<QueryFilterOperateEnum>() {
            @Override
            public QueryFilterOperateEnum fromString(String arg0) {
                return null;
            }

            @Override
            public String toString(QueryFilterOperateEnum queryFilterOperateEnum) {
                return queryFilterOperateEnum.getDesc();
            }
        });
        
        valueTextField = new TextField();
    }
    
    public String getSqlConditionStr() {
        
        Column fieldColumn = fieldChoiceBox.getSelectionModel().getSelectedItem();
        if (Objects.isNull(fieldColumn)) {
            return "";
        }
        
        String fieldName = fieldColumn.getName();
        ColumnTypeEnum fieldType = fieldColumn.getType();
        if (StringUtils.isBlank(fieldName)) {
            return "";
        }
        QueryFilterOperateEnum operate = operateChoiceBox.getSelectionModel().getSelectedItem();
        if (Objects.isNull(operate)) {
            return "";
        }
        String value = valueTextField.getText();
        String sqlValue = this.getSqlValue(fieldType, value);
        
        String queryCondition = "";
        switch (operate) {
	        case BETWEEN:
	    	case NOT_BETWEEN:
	    		String[] values = value.split(","); // TODO
	    		queryCondition = operate.getCode().replace("{0}", fieldName).replace("{1}", values[0]).replace("{2}", values[1]);
	    		break;
	    	case CONTAIN:
	    	case NOT_CONTAIN:	
	    	case START_WITH:
	    	case END_WITH:
        	case IN_LIST:
        	case NOT_IN_LIST:
        		queryCondition = operate.getCode().replace("{0}", fieldName).replace("{1}", value);
        		break;
        	default:
        		queryCondition = operate.getCode().replace("{0}", fieldName).replace("{1}", sqlValue);
        		break;
        }
        
        return queryCondition;
    }
    
    private String getSqlValue(ColumnTypeEnum fieldType, String value) {
        if (fieldType == ColumnTypeEnum.VARCHAR
                || fieldType == ColumnTypeEnum.DATETIME
                || fieldType == ColumnTypeEnum.DATE) {
            return "'" + value + "'";
        } else {
        	return value;
        }
    }
    
    public List<Node> getNodeList() {
        return Arrays.asList(fieldChoiceBox, operateChoiceBox, valueTextField);
    }
}
